var gulp = require('gulp'),
	jshint = require('gulp-jshint');
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
	imagemin = require('gulp-imagemin'),
	jpegtran = require('imagemin-jpegtran'),
	optipng = require('imagemin-optipng'),
	imageminPngquant = require('imagemin-pngquant'),
	cache = require('gulp-cache'),
	notify = require('gulp-notify');
 
 
// 检查脚本
gulp.task('lint', function() {
    gulp.src('src/script/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});
 
gulp.task('testConcat', function () {
    gulp.src('src/script/*.js')
        .pipe(concat('all.js'))//合并后的文件名
        .pipe(gulp.dest('src/dist/scriptMerge'));
});

gulp.task('jsMin', function () {
    gulp.src(['src/dist/scriptMerge/all.js','src/script/a.js','src/script/app.js']) //多个文件以数组形式传入
        .pipe(uglify())
        .pipe(rename(function(path){
            var _time = new Date().getTime();
            console.log(path)
            path.basename = path.basename + ".min";
        }))
        .pipe(gulp.dest('src/dist/scriptMin'));
});

// gulp.task('imgMin', function() {
    // gulp.src('src/images/*.{png,jpg,gif,ico}')
    // .pipe(imagemin({
		// optimizationLevel: 5, //类型：Number  默认：3  取值范围：0-7（优化等级）
		// progressive: true, //类型：Boolean 默认：false 无损压缩jpg图片
		// interlaced: true, //类型：Boolean 默认：false 隔行扫描gif进行渲染
		// multipass: true //类型：Boolean 默认：false 多次优化svg直到完全优化
	// }))
    // .pipe(gulp.dest('img'));
// });

// gulp.task('imgMin', function(){
	// gulp.src('src/images/*')
		// .pipe(imagemin([
			// imagemin.jpegtran({progressive: true}),
			// imagemin.optipng()
		// ],{verbose: true}))
		// .pipe(gulp.dest('src/imagesdest'))
// });

gulp.task('imgMin', function(){
	gulp.src('src/images/*')
		.pipe(imagemin())
		.pipe(gulp.dest('src/imagesdest'))
});

// gulp.task('imgMin', function() {  
  // return gulp.src('src/images/*')
    // .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
    // .pipe(gulp.dest('src/dist/imagesMin'))
    // .pipe(notify({ message: 'Images task complete' }));
// });

gulp.task('default', function(){
	gulp.run('lint','testConcat', 'jsMin', 'imgMin');
	// 监听文件变化
	// gulp.watch('src/script/*.js', function(){
        // gulp.run('lint','testConcat','jsMin','imgMin');
    // });
});